package com.iamteer.day23;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class ChatSender extends Thread{

	@Override
	public void run() {
		try {
			//1. 建立 UDP 服务
			DatagramSocket socket = new DatagramSocket();
			//2. 准备数据
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			String line = null;
			DatagramPacket packet = null;
			while ((line = reader.readLine()) != null) {
				//3. 封装数据
				packet = new DatagramPacket(line.getBytes(), line.getBytes().length, InetAddress.getByName("192.168.1.240"), 9090);
				//4. 发送数据
				socket.send(packet);
			}
			//5. 关闭资源
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
